package cn.mugutu.finance.dto;

import cn.mugutu.finance.domain.system.Role;
import cn.mugutu.finance.domain.system.User;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
public class UserDto implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long id;
    private String username;
    private String realname;
    private boolean locked;        //是否锁定
    private String loginIp;
    private String lastLoginIp;    //上次登录ip
    private Date loginDate;
    private Date lastLoginDate;    //上次登录时间
    private Date createDate;

    private String department;      //所属部门名字
    private Long departmentId;

    private Set<Long> roleIds = new HashSet<Long>();
    private Set<String> roles = new HashSet<String>();


    /**
     * 通过User构造一个UserDto
     *
     * @param user
     */
    public UserDto(User user) {
        super();
        this.id = user.getId();
        this.username = user.getUsername();
        this.realname = user.getRealname();
        this.locked = user.isLocked();
        this.loginIp = user.getLoginIp();
        this.lastLoginIp = user.getLastLoginIp();
        this.loginDate = user.getLoginDate();
        this.lastLoginDate = user.getLastLoginDate();
        this.createDate = user.getCreateDate();

        this.department = user.getDepartment() != null ? user.getDepartment().getName() : "";
        this.departmentId = user.getDepartment() != null ? user.getDepartment().getId() : 1L;

        Set<Role> rs = user.getRoles();
        for (Role role : rs) {
            this.roles.add(role.getName());
            this.roleIds.add(role.getId());
        }
    }

    /**
     * 将List<User> 转化为List<UserDot>
     *
     * @param users
     * @return
     */
    public static List<UserDto> userListToUserDtoList(List<User> users) {
        List<UserDto> userdtoList = new ArrayList<>();
        for (User user : users) {
            UserDto ud = new UserDto(user);
            userdtoList.add(ud);
        }
        return userdtoList;
    }

}
